package com.blb.educourseservice.dldemo;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;

@RestController
public class OrderController {

    //是否支付
    public static boolean payed = false;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/pay")
    public String pay(){
        payed = true;
        return "支付成功";
    }

    @RequestMapping("/notPay")
    public String notPay(){
        payed = false;
        return "拒绝支付";
    }

    @RequestMapping("/buy")
    public String buy(){
        //发送下单的消息，并设置过期时间
        rabbitTemplate.convertAndSend(RabbitMQConfig2.ORDER_EXCHANGE,RabbitMQConfig2.ORDER_QUEUE_KEY,"华为手机",
                message -> {
                    //消息后置处理
                    message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                    //过期时间 30s
                    message.getMessageProperties().setExpiration(String.valueOf(30 * 1000));
                    return message;
                });
        return "下单成功";
    }
}
